package zj.com.activities;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zfdang.multiple_images_selector.SelectorSettings;

import org.json.JSONArray;
import org.json.JSONObject;
import org.xutils.DbManager;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.UUID;

import zj.com.adapter.UploadImgGridAdapter;
import zj.com.db.DBUtil;
import zj.com.model.Gjgx;
import zj.com.model.GjgxDB;
import zj.com.model.JcxDB;
import zj.com.model.Org;
import zj.com.model.Project;
import zj.com.model.UploadFile;
import zj.com.net.NetManager;
import zj.com.net.OnResponseListener;
import zj.com.util.DialogUtil;
import zj.com.util.LogUtils;
import zj.com.util.SharedPreferencesUtil;
import zj.com.zj.R;

/**
 * Created by xi on 16/6/8.
 *
 *质量抓拍
 */
public class ZlzpActivity extends BaseActivity implements View.OnClickListener{
    public TextView title_name,right_tv;
    public ImageView left_iv;

    public GridView uploadGridView;
    public List<String> list;
    public UploadImgGridAdapter adapter;
    public static final int REQUEST_CODE = 732;


    public EditText desc_et;
    public TextView gs_tv,xm_tv;
    public RadioGroup rg;
    public ImageView search_iv;
    public TextView question_et;

    public  static ArrayList<String> mResults = new ArrayList<>();
    public TextView one_tv,two_tv,three_tv,four_tv;
    public RelativeLayout search_rl;

    boolean isAdd = false;
    public LinearLayout gjgx_ll;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zlzp);

        initView();
    }

    public void initView(){
        gjgx_ll = (LinearLayout) findViewById(R.id.gjgx_ll);
        isAdd = getIntent().getBooleanExtra("isAdd",false);

        search_rl = (RelativeLayout) findViewById(R.id.search_rl);
        search_rl.setOnClickListener(this);
        one_tv = (TextView) findViewById(R.id.one_tv);
        two_tv = (TextView) findViewById(R.id.two_tv);
        three_tv = (TextView) findViewById(R.id.three_tv);
        four_tv = (TextView) findViewById(R.id.four_tv);


        desc_et = (EditText) findViewById(R.id.desc_et);
        question_et = (TextView) findViewById(R.id.question_et);
        gs_tv = (TextView) findViewById(R.id.gs_tv);
        gs_tv.setOnClickListener(this);
        xm_tv = (TextView) findViewById(R.id.xm_tv);
        xm_tv.setOnClickListener(this);
        rg = (RadioGroup) findViewById(R.id.rg);
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup arg0, int arg1) {
                // TODO Auto-generated method stub
                //获取变更后的选中项的ID
                int radioButtonId = arg0.getCheckedRadioButtonId();
                if(radioButtonId == R.id.btn_0){
                    status = 0;
                }else if(radioButtonId == R.id.btn_1){
                    status = 1;
                }else if(radioButtonId == R.id.btn_2){
                    status = 2;
                }
                RadioButton rb = (RadioButton) ZlzpActivity.this.findViewById(radioButtonId);


                // tv.setText("您的性别是：" + rb.getText());
            }
        });
        search_iv = (ImageView) findViewById(R.id.search_iv);

        title_name = (TextView) findViewById(R.id.title_name);
        if(isAdd){
            title_name.setText("新增关键工序");

        }else{
            title_name.setText("关键工序修改");

        }
        right_tv = (TextView) findViewById(R.id.right_tv);
        right_tv.setText("保存");
        right_tv.setOnClickListener(this);
        left_iv = (ImageView) findViewById(R.id.left_iv);
        left_iv.setOnClickListener(this);
        left_iv.setVisibility(View.VISIBLE);

        uploadGridView = (GridView) findViewById(R.id.uploadGridView);

        list = new ArrayList<>();
        list.add("");
        adapter = new UploadImgGridAdapter(list,this);
        uploadGridView.setAdapter(adapter);


        gjgxDb = (GjgxDB) getIntent().getSerializableExtra("zlzp");

        if(gjgxDb != null){
            mode = gjgxDb.getMode();
            gs_tv.setText(gjgxDb.getGs());
            xm_tv.setText(gjgxDb.getProject());
            if(gjgxDb.getGjgx() != null){

                String gjgx = gjgxDb.getGjgx();
                String[] gjgxs = gjgx.split(">");

                one_tv.setText("1. "+gjgxs[3]);
                two_tv.setText("2. "+gjgxs[0]);
                three_tv.setText("3. "+gjgxs[1]);
                four_tv.setText("4. "+gjgxs[2]);

            }
            desc_et.setText(gjgxDb.getDesc());

             status = gjgxDb.getStatus();
            if(status == 0){
                rg.check(R.id.btn_0);
            }else if(status == 1){
                rg.check(R.id.btn_1);

            }else{
                rg.check(R.id.btn_2);

            }


            list.clear();
           String img =  gjgxDb.getImgs();
            if(img != null){
                String[] imgs = img.split(",");
                list.clear();
                for(int i=0;i<imgs.length;i++){
                    list.add(imgs[i]);
                }


            }
            list.add("");
        }else{
            gjgx_ll.setVisibility(View.GONE);
        }
        adapter.setDataChange(list);

        if(mode == 1){
            search_iv.setVisibility(View.GONE);
        }else{
            search_iv.setVisibility(View.VISIBLE);

        }

    }

   public GjgxDB gjgxDb;
    public Org gs;

    public Project xm;
    public Gjgx gjgx;
    public int mode;
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if(resultCode == RESULT_OK) {

            if(requestCode == GS_CODE){

                gs = (Org) data.getSerializableExtra("org");
                if(gs != null){
                    gs_tv.setText(gs.getOrgName());
                }
                return;
            }else if(requestCode == XM_CODE){
                xm = (Project) data.getSerializableExtra("project");
                if(xm != null){
                    xm_tv.setText(xm.getProjectName());
                }
                return;
            }else if(requestCode == GJGX_CODE){
                 gjgx = (Gjgx) data.getSerializableExtra("gjgx");
                if(gjgx != null){
                    one_tv.setText("1."+gjgx.getName());
                    two_tv.setText("2. "+gjgx.getUnitProject());
                    three_tv.setText("3. "+gjgx.getPartProject());
                    four_tv.setText("4. "+gjgx.getSpecialProject());
                    gjgx_ll.setVisibility(View.VISIBLE);
//                    question_et.setText(gjgx.getUnitProject()+" > "+gjgx.getPartProject()+" > "+gjgx.getSpecialProject());
                }
            }


        }
        // get selected images from selector
        if(requestCode == REQUEST_CODE) {
            if(resultCode == RESULT_OK) {
                mResults = data.getStringArrayListExtra(SelectorSettings.SELECTOR_RESULTS);
                assert mResults != null;

                // show results in textview
                StringBuilder sb = new StringBuilder();
                sb.append(String.format("Totally %d images selected:", mResults.size())).append("\n");
                list.clear();
                for(String result : mResults) {
                    list.add(result);
                    sb.append(result).append("\n");

                }
                Log.e("wxf", sb.toString());
                list.add("");
                adapter.setDataChange(list);
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    public Intent intent;
    public final int GS_CODE = 0x2210;
    public final int XM_CODE = GS_CODE+1;
    public final int GJGX_CODE = GS_CODE+2;
    public int status = 0;
    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.left_iv:
                finish();
                break;
            case R.id.right_tv:
                add();
                break;
            case R.id.gs_tv:

                if(mode == 1) return;
                intent = new Intent(ZlzpActivity.this,GsSelectActivity.class);
                intent.putExtra("type",0);
                startActivityForResult(intent, GS_CODE);
                break;
            case R.id.xm_tv:
                if(mode == 1) return;

                intent = new Intent(ZlzpActivity.this,XmSelectActivity.class);
                intent.putExtra("type", 1);
                intent.putExtra("isMuti",false);
                intent.putExtra("org",gs);
                startActivityForResult(intent, XM_CODE);
                break;
            case R.id.search_rl:

                intent = new Intent(ZlzpActivity.this,GjgxSearchActivity.class);
                intent.putExtra("project",xm);
                startActivityForResult(intent,GJGX_CODE);
                break;

        }
    }


    public int count = 0;
    public ProgressDialog dialog;
    public void add(){

        String desc = desc_et.getText().toString();
        if(gjgxDb == null) {
            if (gs == null) {
                Toast.makeText(ZlzpActivity.this, "请选择公司", Toast.LENGTH_SHORT).show();
                return;
            }
            if (xm == null) {
                Toast.makeText(ZlzpActivity.this, "请选择项目", Toast.LENGTH_SHORT).show();
                return;
            }
            if (gjgx == null) {
                Toast.makeText(ZlzpActivity.this, "请选择施工工序", Toast.LENGTH_SHORT).show();
                return;
            }
            if (TextUtils.isEmpty(desc)) {
                Toast.makeText(ZlzpActivity.this, "请输入描述", Toast.LENGTH_SHORT).show();
                return;
            }

            if (list.size() == 1) {
                Toast.makeText(ZlzpActivity.this, "请选择图片", Toast.LENGTH_SHORT).show();
                return;
            }
        }

        StringBuffer sb = new StringBuffer();
        for(int i=0;i<list.size();i++){

            if(i != list.size() -1){


                sb.append(list.get(i));
                sb.append(",");
            }
        }
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
        String dateTime = sdf.format(calendar.getTime());
        DbManager dbManager = DBUtil.getInstance(this).getDBManager();
        GjgxDB db = new GjgxDB();
        if (gjgxDb != null) {

            db.setID(gjgxDb.getID());
        }else{
            db.setID(UUID.randomUUID().toString());

        }

        if(gjgxDb != null){

            db.setGjgxId(gjgxDb.getGjgxId());
            db.setStatus(status);
            db.setGs(gjgxDb.getGs());
            db.setProject(gjgxDb.getProject());
            db.setGjgx(gjgxDb.getGjgx());
            db.setImgs(sb.toString());
            db.setDesc(desc);
            db.setTime(dateTime);
        }else{
            db.setGjgxId(gjgx.getID() + "");
            db.setStatus(status);
            db.setGs(gs.getOrgName());
            db.setDesc(desc);
            db.setProject(xm.getProjectName());
            db.setGjgx(gjgx.getUnitProject() + " > " + gjgx.getPartProject() + " > " + gjgx.getSpecialProject()+" > "+gjgx.getName());
            db.setImgs(sb.toString());
            db.setTime(dateTime);

        }

        try {
            dbManager.saveOrUpdate(db);
            Toast.makeText(ZlzpActivity.this,"保存成功",Toast.LENGTH_SHORT).show();
            finish();

        }catch (Exception e){

        }




    }


}
